﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

public partial class ProductManage_BothProductManage_ClientSoftwareAdd : System.Web.UI.Page
{
    private ClientSoftware csw;

    public ProductManage_BothProductManage_ClientSoftwareAdd()
    {
        csw = new ClientSoftware();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindClientSoftwares();
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        string cSName = TextBox1.Text.ToString().Trim();
        string str1 = "客户端软件名称：" + cSName + "已存在!";
        string str2 = cSName + "添加成功!";
        string str3 = "添加失败，请联系相关人员!";

        if (!string.IsNullOrEmpty(csw.GetClientSoftwarebyName(cSName).CSName))
        {
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else
        {
            if (csw.Insert(new ClientSoftwareInfo() { CSID = 0, CSName = cSName }))
            {
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str2 + "');</script>", false);
                this.TextBox1.Text = "";
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str3 + "');</script>", false);
            }
        }
        BindClientSoftwares();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[1].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
            }
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int cSID = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
        csw.Delete(cSID);
        BindClientSoftwares();
    }

    protected void BindClientSoftwares()
    {
        GridView1.DataSource = csw.LoadEntities();
        GridView1.DataBind();
    }
}
